<?php

	// +----------------------------------------------------------------------
	// | http异常处理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\exception\api;

	use think\exception\Handle;
	use think\exception\HttpException;
	use think\exception\ValidateException;
	use think\Response;
	use Throwable;

	class Http extends Handle
	{
		public function render($request, Throwable $e): Response
		{
			if ($e instanceof HttpException) {
				$statusCode = $e->getStatusCode();
			}

			if (!isset($statusCode)) {
				$statusCode = 500;
			}
            if (str_starts_with($request->controller(), 'api'))
            {
                $result = [
                    'code' => $statusCode,
                    'message' => $e->getMessage(),
                    'file' => $e->getFile(),
                    'line' => $e->getLine(),
                ];
                return response_ajax($result, $statusCode);
            }else{
                return parent::render($request, $e);
            }


			// 参数验证错误
//			if ($e instanceof ValidateException) {
//				return json($e->getError(), 422);
//			}
//
//			// 请求异常
//			if ($e instanceof HttpException && $request->isAjax()) {
//				return response($e->getMessage(), $e->getStatusCode());
//			}
//
//			// 其他错误交给系统处理
//			return parent::render($request, $e);

		}

	}
